package com.lhdx.practical.dao;

import com.lhdx.practical.bean.Content;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

import javax.transaction.Transactional;
import java.util.List;

@Repository
public interface ContentDao extends CrudRepository<Content,Long> {
    void deleteById(long id);

    Content findById(long id);

    List<Content> findAllBySid(long sid);
    List<Content> findAllBySidAndMidAndGradeid(long sid,long mid,long gradeid);

    Content findByGradeidAndSid(long gradeid,long sid);

    Page<Content> findAll(Specification<Content> spec, Pageable pageable);

    /**
     * 修改点击数
     * @param id
     * @param clicks
     */
    @Transactional
    @Modifying(clearAutomatically = true)
    @Query(value = "update content set clicks = ?2 where id = ?1",nativeQuery = true)
    void updateClicks(long id,int clicks);

    /**
     * 修改下载数
     * @param id
     * @param downloadcount
     */
    @Transactional
    @Modifying(clearAutomatically = true)
    @Query(value = "update content set downloadcount = ?2 where id = ?1",nativeQuery = true)
    void updateDownloads(long id,int downloadcount);
}
